package com.sh.mytool.cv.service.impl;

import java.util.LinkedHashMap;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.sh.common.page.QueryResult;
import com.sh.mytool.cv.dao.BasicDao;
import com.sh.mytool.cv.domain.Video;
import com.sh.mytool.cv.domain.VideoFile;
import com.sh.mytool.cv.domain.VideoImage;
import com.sh.mytool.cv.service.VideoService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service("videoService")
public class VideoServiceImpl implements VideoService {

    @Resource(name="basicDao")
    private BasicDao<Video> dao;

    public BasicDao<Video> getDao() {
        return dao;
    }

    public void setDao(BasicDao<Video> dao) {
        this.dao = dao;
    }

    @Override
    @Transactional(propagation=Propagation.NOT_SUPPORTED, readOnly=true)
    public QueryResult<Video> page(String scope, Object[] params, int begin, int max) {
        LinkedHashMap<String, Object> where = new LinkedHashMap<String, Object>();
        LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
        return dao.getScrollData(Video.class, begin, max, where, orderby);
    }

    @Override
    @Transactional(propagation=Propagation.REQUIRED)
    public void insert(Video video, VideoFile videoFile, VideoImage imageFile) throws Exception {
        if(videoFile != null){
            videoFile.setVideo(video);
            video.setVideoFile(videoFile);
        }
        if(imageFile != null){
            imageFile.setVideo(video);
            video.setVideoImage(imageFile);
        }
        dao.insert(video);
    }

    @Override
    @Transactional(propagation=Propagation.REQUIRED)
    public void update(Video video, VideoFile videoFile, VideoImage imageFile) throws Exception {
        Video oldBean = this.get(video.getId());
        oldBean.setName(video.getName());
        if(videoFile != null){
            oldBean.getVideoFile().setVideo(null);
            videoFile.setVideo(oldBean);
            oldBean.setVideoFile(videoFile);
        }
        if(imageFile != null){
            oldBean.getVideoImage().setVideo(null);
            imageFile.setVideo(oldBean);
            oldBean.setVideoImage(imageFile);
        }
        dao.update(oldBean);
    }

    @Override
    @Transactional(propagation=Propagation.REQUIRED)
    public void delete(Long id) throws Exception {
        dao.deleteById(Video.class, id);
    }

    @Override
    @Transactional(propagation=Propagation.NOT_SUPPORTED, readOnly=true)
    public Video get(Long id) {
        return dao.findById(Video.class, id);
    }



}
